package com.yesido.javabase.io.demo2;

import com.yesido.javabase.io.FileConstant;

import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

/**
 * @author yesido
 * @description
 * @date 2023-06-28 16:36
 * @Version 8.3
 */
public class FileReadWriteExample {
    public static void main(String[] args) {
        Path sourceFilePath = Paths.get(FileConstant.IN_FILE_PATH);
        Path targetFilePath = Paths.get(FileConstant.OUT_FILE_PATH);

        // 从源文件读取并写入目标文件
        try (FileChannel sourceChannel = FileChannel.open(sourceFilePath, StandardOpenOption.READ);
             FileChannel targetChannel = FileChannel.open(targetFilePath, StandardOpenOption.CREATE, StandardOpenOption.WRITE)) {

            sourceChannel.transferTo(0, sourceChannel.size(), targetChannel);

            System.out.println("文件读写成功！");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
